//操作Cookie
var Cookies = new Object();
Cookies.set = function(name, value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
};

Cookies.get = function(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while (i < clen) {
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return Cookies.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
};

Cookies.clear = function(name) {
    if (Cookies.get(name)) {
        document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
};

Cookies.getCookieVal = function(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
};
//操作Cookie

function reset() {
    $('#username').val('');
    $('#password').val('');
    $('.erorrmsg').hide();
}

//回车确认事件
document.onkeydown = function(event) {
    if (event.keyCode == 13) {
        $(".loginbutton").trigger("click");
    }
}
var loginUser = new Object();
var isLogin = new Object(); //用来判断是否为登录中
function login() {



    $('.erorrmsg').hide();
    var username = $("#username").val();
    var password = $("#password").val();
    localStorage.setItem("accout1", username);
    localStorage.setItem("password", password);
    var url = '/rest/v2/login';
    $.ajax({
        url: url,
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify({
            "account": username,
            "password": password
        }),
        success: function(data) {
            var sunccess = data.message=="OK";
            if (sunccess == true) {
                localStorage.removeItem("indexeuid");
                loginUser = data.result;
                loginUser["account"] = username; /**/
                localStorage.setItem("account", username);
                Cookies.set('loginUser', JSON.stringify(loginUser));
                localStorage.setItem('isLogin', JSON.stringify(loginUser));
                window.location.href = "home.html";
            } else {
                $('.erorrmsg').show();
            }
        },
        error: function(result) {

        }
    });
}

